/*
 * The MIT License (MIT)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package multiswarm.examples;

import java.util.Random;

import com.badlogic.gdx.graphics.Color;

import multiswarm.Agent;
import multiswarm.AgentJoint;
import multiswarm.AgentSensor;
import multiswarm.Arena;

public class FireflyAgent extends Agent {
	private Random rand = new Random();

	float random_number = rand.nextFloat();
	float wobble = random_number;
	boolean goingUp = rand.nextBoolean();
	
	private static final int SENSOR = 0;

	public FireflyAgent(Arena arena) {
		super(arena);
		
		//Make our agents a little bit smaller.
		setScale(0.35f);
		
		//Enable light emitter
		enableLight = true;
				
		//Creates a 360degree sensor around the agent.
		addSensor(new AgentSensor(this, SENSOR, 10, 360, 0));
		
		//Set initial random color
		if(rand.nextBoolean()) {
		  setColor(Color.BLUE);
		} else {
		  setColor(Color.RED);
		}
	}

	@Override
	public void agentAct(float delta) {
		//Movement:
    	goForward(random_number*250f);
		turnLeft(rand.nextFloat()/4);
		
		//Update blur: 
		setBlur(wobble*0.2f);
		
		//Update light intensity:
		light.setDistance(Math.abs(wobble*100f));
		
		//Agent behavior logic:
		if (goingUp) turnLeft(wobble+=0.005f);
	    else         turnRight(wobble-=0.05f);
	      
	    if (wobble>0.6) {
	      setColor(Color.BLUE);
	      light.setColor(Color.BLUE);
	      goingUp=false;
	    } else if (wobble<-0.3) {
	      setColor(Color.RED);
	      light.setColor(Color.RED);
	      goingUp=true;
	    }
	    
	    //Sync rotation with connected agents:
	    for (Agent agent: directlyConnectedAgents) {	 
	    	FireflyAgent fAgent = (FireflyAgent) agent;
	    	if (fAgent.rotation<this.rotation) turnRight(0.01f); else turnLeft(0.01f);
	    	if (fAgent.wobble<this.wobble) this.wobble-=0.00002f; else this.wobble+=0.00002f;
	    }
	     
		//Iterate over all agents in sensor cone and connect with them via joint
    	for (Agent agent: getSensor(SENSOR).agents) {
    		//Note: this rules out duplicate connections automatically.
			createJointWithAgent(agent, 55*getScaleX(), 10);
    	}  
	}
}
